package com.carkeeper.shop.controller;

import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.pojo.ServiceType;
import com.carkeeper.shop.service.TypeService;
import com.github.pagehelper.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/type")
@Api(tags = "服务分类接口")
public class TypeController {
    @Autowired
    private TypeService typeService;

    /**
     * 添加服务分类
     */
    @PostMapping("/add")
    @ApiOperation("添加分类接口")//标注方法
    private Result addServiceType(@RequestBody ServiceType serviceType){
        typeService.addServiceType(serviceType);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改服务分类
     * @param serviceType
     */
    @PutMapping("/update")
    @ApiOperation("修改分类接口")//标注方法
    private Result updateServiceType(@RequestBody ServiceType serviceType){
        typeService.updateServiceType(serviceType);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 查询所有分类
     * @return
     */
    @GetMapping("/{page}/{size}")
    @ApiOperation("分页查询接口")
    private Result<PageResult<ServiceType>> findPage(@PathVariable("page") int page,@PathVariable("size") int size){
        Page<ServiceType> pageList = typeService.findTypePage(page, size);
        PageResult<ServiceType> pageResult = new PageResult<>(pageList.getTotal(), pageList.getResult());
        return new Result<>(true, StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 多条件分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    @ApiOperation("条件分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code"),
            @ApiImplicitParam(name = "name")
    })
    private Result<PageResult<ServiceType>> findPage(@RequestParam Map<String,Object> searchMap,@PathVariable("page") int page,@PathVariable("size")int size){
        Page<ServiceType> pageList = typeService.findTypePage(searchMap, page, size);
        PageResult<ServiceType> pageResult = new PageResult<>(pageList.getTotal(),pageList.getResult());
        return new Result<>(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 删除服务分类
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除分类接口")
    private Result deleteTypeById(@PathVariable("id") String id){
        try {
            typeService.deleteTypeById(id);
            return new Result(true,StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"无法删除");
        }
    }
}
